<template>
  <view class="vip-dialog-mask" v-if="visible" @click="close">
    <view class="vip-dialog-container" @click.stop>
      <view class="vip-dialog-header">
        <text class="title">开通VIP</text>
        <text class="close" @click="close">×</text>
      </view>
      
      <view class="vip-dialog-content">
        <!-- <image class="vip-icon" src="/static/vip-icon.png" mode="aspectFit"></image> -->
        
        <view class="comparison-section">
          <view class="comparison-header">
            <text class="comparison-title">权益对比</text>
          </view>
          <view class="comparison-row">
            <text class="comparison-label">每日查看宝贝信息次数</text>
            <view class="comparison-values">
              <text class="comparison-value non-vip">1次</text>
              <text class="comparison-value vip">无限</text>
            </view>
          </view>
          <view class="divider"></view>
        </view>
        
        <view class="benefits-container">
          <view class="benefits-column">
            <text class="column-title">普通用户</text>
            <view class="benefits">
              <view class="benefit-item">
                <text class="icon non-vip-icon">×</text>
                <text class="text non-vip-text">去除所有广告</text>
              </view>
              <view class="benefit-item">
                <text class="icon non-vip-icon">×</text>
                <text class="text non-vip-text">无限次换宝贝</text>
              </view>
              <view class="benefit-item">
                <text class="icon non-vip-icon">×</text>
                <text class="text non-vip-text">专属VIP标识</text>
              </view>
              <view class="benefit-item">
                <text class="icon non-vip-icon">×</text>
                <text class="text non-vip-text">动态只能看前3条</text>
              </view>
              <view class="benefit-item">
                <text class="icon non-vip-icon">×</text>
                <text class="text non-vip-text">每月赠送积分</text>
              </view>
            </view>
          </view>
          
          <view class="benefits-column">
            <text class="column-title">VIP用户</text>
            <view class="benefits">
              <!-- <view class="benefit-item" v-for="(item, index) in benefits" :key="'vip-'+index">
                <text class="icon">✓</text>
                <text class="text">{{ item }}</text>
              </view> -->
				<view class="benefit-item">
			      <text class="icon">✓</text>
			      <text class="text">去除所有广告</text>
			    </view>
			    <view class="benefit-item">
			      <text class="icon">✓</text>
			      <text class="text">无限次换宝贝</text>
			    </view>
			    <view class="benefit-item">
			      <text class="icon">✓</text>
			      <text class="text">专属VIP标识</text>
			    </view>
			    <view class="benefit-item">
			      <text class="icon">✓</text>
			      <text class="text">无限次宝贝动态</text>
			    </view>
			    <view class="benefit-item">
			      <text class="icon">✓</text>
			      <text class="text">每月赠送30积分</text>
			    </view>
            </view>
          </view>
        </view>
      </view>
      
      <view class="vip-dialog-footer">
        <view class="price">¥ {{ price }}<text class="month">/月</text></view>
        <button class="pay-button" @click="handlePay">立即开通</button>
      </view>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import request from '@/utils/request.js'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  price: {
    type: Number,
    default: 30.00
  }
})

const emit = defineEmits(['update:visible', 'pay'])

const orderType = ref(0)
const balance = ref(0)
const orderId = ref(0)
const paySource = ref(0)
const loading = ref(true)

const close = () => {
  emit('update:visible', false)
}

const handlePay = () => {
	loading.value = true;
	uni.showLoading({
		title: '加载中'
	});
	let url = 'p/order/noOrderPay';
	let params = {
		orderNumbers: orderId.value,
		paySource: 1,
		payType: 1,
		useBalance: balance.value,
		money: props.price
	};
	request.post("p/order/noOrderPay", params)
	.then(res => {
		loading.value = false;
		uni.hideLoading();
		console.log(res);
		if(res.success == true){
			handleAlipayResponse(res)
		}else{
			debugger
			// pay(res, self, paySuccess, payError);					
		}
	})
}
const paySuccess = (result) => {
	// this.gotoPage('/pages/order/pay-success/pay-success?orderId=' + result.data.orderId, 'reLaunch');
	uni.navigateTo({
		url: '/pages/order/pay-success/pay-success?orderId=' + result.data.orderId
	});
}
const payError = (result) => {
	// gotoPage('/pages/order/order-detail?orderId=' + result.data.orderId, 'redirect');
	uni.navigateTo({
		url: '/pages/order/order-detail?orderId=' + result.data.orderId
	});
}

const handleAlipayResponse = (response) => {
  // 1. 直接使用原始payment数据，避免任何处理
  const paymentData = response.data;
		  
  // console.log("paymentData:"+paymentData)
  
  // // 2. 创建极简HTML，确保无额外空格或换行
  // const html = `<!DOCTYPE html><html><head>
  //       <meta charset="UTF-8">
  //       <meta name="viewport" content="width=device-width, initial-scale=1.0">
  //       <title>正在跳转支付宝...</title>
  //     </head><body>${response.data.payment}</body></html>`;
	  
  // console.log("html:"+html)
  
  // // 3. 创建Blob时指定正确编码
  // const blob = new Blob([html], {type: 'text/html;charset=utf-8'});
  // const url = URL.createObjectURL(blob);
  
  // 4. 导航到webview页面
  uni.navigateTo({
	url: `/pages/webview/webview?url=${encodeURIComponent(paymentData)}`
  });
}
</script>

<style scoped>
.vip-dialog-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 999;
}

.vip-dialog-container {
  width: 80%;
  max-width: 600rpx;
  background-color: #fff;
  border-radius: 16rpx;
  overflow: hidden;
  box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.2);
}

.vip-dialog-header {
  padding: 30rpx;
  background: linear-gradient(to right, #ff9500, #ff5e3a);
  color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.vip-dialog-header .title {
  font-size: 36rpx;
  font-weight: bold;
}

.vip-dialog-header .close {
  font-size: 50rpx;
  line-height: 1;
}

.vip-dialog-content {
  padding: 30rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.vip-icon {
  width: 120rpx;
  height: 120rpx;
  margin-bottom: 30rpx;
}

.comparison-section {
  width: 100%;
  margin-bottom: 30rpx;
}

.comparison-header {
  margin-bottom: 20rpx;
}

.comparison-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.comparison-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15rpx;
}

.comparison-label {
  font-size: 28rpx;
  color: #666;
  flex: 1;
}

.comparison-values {
  display: flex;
  width: 200rpx;
}

.comparison-value {
  flex: 1;
  text-align: center;
  font-size: 28rpx;
  padding: 5rpx 10rpx;
  border-radius: 8rpx;
}

.comparison-value.non-vip {
  background-color: #f5f5f5;
  color: #999;
}

.comparison-value.vip {
  background-color: #fff8e6;
  color: #ff9500;
  font-weight: bold;
}

.divider {
  height: 1rpx;
  background-color: #f0f0f0;
  margin: 20rpx 0;
}

.benefits-container {
  width: 100%;
  display: flex;
  justify-content: space-between;
  margin-top: 20rpx;
}

.benefits-column {
  width: 48%;
}

.column-title {
  display: block;
  text-align: center;
  font-size: 30rpx;
  font-weight: bold;
  margin-bottom: 15rpx;
  padding-bottom: 10rpx;
  border-bottom: 1px solid #f0f0f0;
}

.benefits-column:first-child .column-title {
  color: #999;
}

.benefits-column:last-child .column-title {
  color: #ff9500;
}

.benefits {
  width: 100%;
}

.benefit-item {
  display: flex;
  align-items: center;
  padding: 15rpx 0;
  border-bottom: 1rpx solid #f0f0f0;
}

.benefit-item .icon {
  color: #ff9500;
  margin-right: 15rpx;
  font-weight: bold;
}

.benefit-item .non-vip-icon {
  color: #ccc;
}

.benefit-item .text {
  color: #333;
  font-size: 28rpx;
}

.benefit-item .non-vip-text {
  color: #999;
}

.vip-dialog-footer {
  padding: 30rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-top: 1rpx solid #f0f0f0;
}

.price {
  color: #ff5e3a;
  font-size: 48rpx;
  font-weight: bold;
}

.price .month {
  font-size: 24rpx;
  color: #999;
  margin-left: 5rpx;
}

.pay-button {
  background: linear-gradient(to right, #ff9500, #ff5e3a);
  color: white;
  border: none;
  border-radius: 50rpx;
  padding: 0 40rpx;
  height: 80rpx;
  line-height: 80rpx;
  font-size: 28rpx;
}

.pay-button::after {
  border: none;
}
</style>